<?php
namespace Application\Api\Controller;

class Reimbursement extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_REIMBURSEMENT);
    }

    public function indexAction()
    {
        $data = $this->getRequestData();
        $send = $this->getService()->getIndex($data['page'], $data['limit']);
        $this->sendSuccess($send);
    }

    public function newAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $data['dry_sort'] = isset($data['dry_sort']) ? $data['dry_sort'] : 0;
        $data['dry_status'] = 1;
        $id = $service->add($data);
        $send = [
            'id' => $id
        ];
        $this->sendSuccess($send);
    }

    public function editAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $rows = $service->update($data, $id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function getListByTeamAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $reim['dry_team'] = $data['dry_team'];
        $temp = $this->getService()->getMoreByFields($reim);
        $list = $this->get(SERVICE_OSSFILE)->getFileData($temp, ['dry_bill' => 2,'dry_reim_certify' => 1]);
        $data = [
            'list' => $list,
        ];
        $this->sendSuccess($data);
    }

    public function getListByFieldsAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $teams = [];
        $employee = [];
        if($data['id']){
            $reim['id'] = $data['id'];
            $temp = $this->getService()->getMoreByFields($reim);
            $list = $this->get(SERVICE_OSSFILE)->getFileData($temp, ['dry_bill' => 2,'dry_reim_certify' => 1]);
        }
        else{
            $employee = $this->get(SERVICE_EMPLOYEE)->getMoreByField("dry_employee",$userId);
            $teams = $this->get(SERVICE_TEAM)->getMoreByField("dry_user",$userId);
            $teamIds = [];
            foreach($employee as $v){
                array_push($teamIds,$v->dry_team);
            }
            foreach($teams as $v){
                array_push($teamIds,$v->id);
            }
            $fieldsIn = implode(',', $teamIds);

//        unset($data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign']);
            $temp = $this->getService()->getListByFieldsIn('dry_team', $fieldsIn);
            $list = $this->get(SERVICE_OSSFILE)->getFileData($temp, ['dry_bill' => 2,'dry_reim_certify' => 1]);
        }
        $data = [
            'list' => $list,
            'teams' => $teams,
            'employee' => $employee
        ];
        $this->sendSuccess($data);
    }

    /*
        /Api/Reimbursement/getUploadData
    */
    public function getUploadDataAction()
    {
        $data = $this->get(SERVICE_OSS)->getUploadData('spwork');
        $this->sendSuccess($data);
    }
}